package client;

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

import data.Group;




import server.ChatServer;
import server.ChatServerImp;

public class ChatClient extends UnicastRemoteObject {

	public final static int NORMAL = 0;
	//public final static int MASTER = 1;
	private ChatServer chatServer;
	private TextArea messageBox;
	private JFrame userFrame;
	
	private String userName;
	
	private String title;
	private String address="//localhost/ChatServer";
	public TextArea getMessageBox() {
		return messageBox;
	}

	public ChatClient(String username,Group g) throws RemoteException{
		this.userName=username;
		this.address="//localhost/ChatServer"+g.getId();
		title="Group:"+g.getName();
		init();
	}
	public ChatClient() throws RemoteException {
		init();
	}
	private void init(){
		createFrame();
		try {
			chatServer = (ChatServer) Naming.lookup(address);

			int status;
			// create a CallBack to allow user to control the client
			CallBackImp CallBackControl = new CallBackImp(this);
			do {
				if(userName==null){
					userName = getUserName();
				}
				status = chatServer.addChatListener((CallBack) CallBackControl,
						userName);
			} while (status != ChatServerImp.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/*
	 * getUserName prompts the user for their desired username. It will do this
	 * until it receives a suitable string. It will return the username upon
	 * success.
	 */

	public String getUserName() {
		JFrame inputFrame = new JFrame();
		String name = null;
		while (name == null)
			name = JOptionPane.showInputDialog(inputFrame, "Enter a username",
					"ChatClient", JOptionPane.QUESTION_MESSAGE);
		return name;
	}

	/* createFrame builds the main GUI */
	public void createFrame() {

		

		
		JFrame chatFrame = new JFrame(title);

		chatFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				try {
					chatServer.removeChatListener(userName, "Leaving");
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});

		Container chatContainer = chatFrame.getContentPane();

		JScrollPane sp = new JScrollPane(messageBox = new TextArea(40, 10));
		messageBox.setEditable(false);
		chatContainer.add(messageBox, BorderLayout.CENTER);

		JPanel controlPanel = new JPanel();
		final TextField inputText = new TextField(40);
		controlPanel.add(inputText);
		JButton sendButton = new JButton("Send Message");
		sendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					chatServer.sendChatMessage(userName, inputText.getText());
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				inputText.setText("");
			}
		});
		controlPanel.add(sendButton);

		JButton exitButton = new JButton("Exit Chat");
		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					chatServer.removeChatListener(userName, "Leaving");
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
		controlPanel.add(exitButton);

		chatContainer.add(controlPanel, BorderLayout.SOUTH);

		chatFrame.setSize(540, 400);
		chatFrame.pack();

		/* This is apparently not available in the JRE available on nova. */
		// chatFrame.setLocationRelativeTo(null);
		chatFrame.setVisible(true);
	}

	/*
	 * User to close the userFrame through the listener object attached to the
	 * buttons.
	 */
	public void close() {
		userFrame.setVisible(false);
	}

	public static void main(String argv[]) {
		try {
			Group g=new Group();
			g.setId(1);g.setName("Group");
			new ChatClient("abc",g);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}